﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FastReflectionLib;
using System.Reflection;
using SZHL.Data;
using Newtonsoft.Json;
using SZHL.UI.AppCode;
using System.Data;

namespace SZHL.UI.API
{
    public class JFBXManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(JFBXManage).GetMethod(msg.Action.ToUpper());
            JFBXManage model = new JFBXManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }
        /// <summary>
        /// 添加经费报销
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">经费报销实体json</param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void ADDJFBX(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_JFBX jfbx = JsonConvert.DeserializeObject<ZH_JFBX>(P1);
            if (string.IsNullOrEmpty(jfbx.ItemIds))
            {
                msg.ErrorMsg = "请选择经费报销详细";
                return;
            }
            if (P2 != "") // 处理微信上传的图片
            {
                string fids = CommonHelp.ProcessWxIMG(P2, UserInfo.User.UserName);
                if (!string.IsNullOrEmpty(jfbx.Files))
                {
                    jfbx.Files += "," + fids;
                }
                else
                {
                    jfbx.Files = fids;
                }
            }
            if (jfbx.ID == 0)
            {
                jfbx.ShenQingRen = string.IsNullOrEmpty(jfbx.ShenQingRen) ? UserInfo.User.UserRealName : jfbx.ShenQingRen;
                jfbx.CRDate = DateTime.Now;
                jfbx.CRUser = UserInfo.User.UserName;
                jfbx.BranchName = new JH_Auth_BranchB().GetBranchNameByCode(UserInfo.User.BranchCode);
                jfbx.BranchNo = UserInfo.User.BranchCode;
                jfbx.FormCode = new ZH_JFBXB().GetFormCode("ZH_JFBX");
                new ZH_JFBXB().Insert(jfbx);
                string strSQL = string.Format("update ZH_JFBX_Item set status=1 where  Id in ({0})", jfbx.ItemIds);
                new ZH_JFBXB().ExsSql(strSQL);
                string jsr = new Yan_WF_TIB().GetEntities(d => d.PIID.Value == jfbx.intProcessStanceid && d.EndTime == null).ToList()[0].TaskUserID;
                if (!string.IsNullOrEmpty(jsr))
                {
                    //发送消息
                    string content = UserInfo.User.UserRealName + "发起了一个经费报销单，请您查阅审核";
                    new JH_Auth_User_CenterB().SendMsg(UserInfo, "jfbx", "经费报销", content, jfbx.ID, jsr, jfbx.intProcessStanceid);
                }
            }
            else
            {
                new ZH_JFBXB().Update(jfbx);
            }
            msg.Result = jfbx;
        }
        /// <summary>
        /// 添加报销经费详细
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void ADDJFBXITEM(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {

            ZH_JFBX_Item item = JsonConvert.DeserializeObject<ZH_JFBX_Item>(P1);

            if (item.ID == 0)
            {
                item.CRDate = DateTime.Now;
                item.CRUser = UserInfo.User.UserName;
                item.status = 0;
                item.ShenQingRen = UserInfo.User.UserRealName;
                new ZH_JFBX_ItemB().Insert(item);
            }
            else
            {
                new ZH_JFBX_ItemB().Update(item);
            }
            msg.Result = item;
        }
        /// <summary>
        /// 获取要添加经费报销列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void GETJFBXITEMBYIDS(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            List<ZH_JFBX_Item> list = null;
            if (P1 == "")
            {
                list = (List<ZH_JFBX_Item>)new ZH_JFBX_ItemB().GetEntities(d => d.status == 0 && d.CRUser == UserInfo.User.UserName);
            }
            else
            {
                string[] Ids = P1.Split(',');
                list = (List<ZH_JFBX_Item>)new ZH_JFBX_ItemB().GetEntities(d => Ids.Contains(d.ID.ToString()) && d.status == 0);

            }
            msg.Result = list;
            msg.Result1 = list.Sum(d => d.BXJE);
            string itemIds = "";
            foreach (ZH_JFBX_Item item in list)
            {
                itemIds += item.ID + ",";
            }
            itemIds = string.IsNullOrEmpty(itemIds) ? "" : itemIds.Substring(0, itemIds.Length - 1);
            msg.Result2 = itemIds;
        }
        /// <summary>
        /// 获取经费报销内容列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void GETJFBXITEMLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            msg.Result = new ZH_JFBX_ItemB().GetEntities(d => d.status == 0 && d.CRUser == UserInfo.User.UserName);
        }
        /// <summary>
        /// 获取经费报销列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">报销内容</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETUSERJFBXLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strSql = string.Format(@"SELECT jf.*, case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                                            when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName
                                            from ZH_JFBX jf inner join  Yan_WF_PI wfpi  on jf.intProcessStanceid=wfpi.ID  ");
            string strWhere = " Where 1=1 ";
            string branch = context.Request.QueryString["b"] ?? "";
            if (branch != "")
            {
                string strQueryUser = context.Request["QUSER"] ?? UserInfo.User.UserName;
                if (strQueryUser != "")
                {
                    strWhere += string.Format(" And  jf.CRUser in ('{0}')", strQueryUser.ToFormatLike());
                }
                else
                {
                    strWhere += string.Format(" And  jf.BranchNo = '{0}'", UserInfo.User.BranchCode);
                }
            }
            else
            {
                strWhere += string.Format(" And jf.CRUser='{0}' ", UserInfo.User.UserName);
            }
            if (P1 != "")
            {
                strWhere += string.Format("and jf.BXContent like '%{0}%'", P1);
            }
            msg.Result = new ZH_JFBXB().GetDTByCommand(strSql + strWhere);
        }
        /// <summary>
        /// 经费报销的数量
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETJFBXMENUCOUNT(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strSql = "select Count(0) from ZH_JFBX";
            string strWhere = string.Format(" where  BranchNo= '{0}'", UserInfo.User.BranchCode);
            msg.Result = new ZH_GZRZB().ExsSclarSql(strSql + strWhere);
            strWhere = string.Format(" where CRUser= '" + UserInfo.User.UserName + "'");
            msg.Result1 = new ZH_GZRZB().ExsSclarSql(strSql + strWhere);

        }
        /// <summary>
        /// 获取经费报销列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">报销内容</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETUSERJFBXLIST_PAGE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = " ";
            string branch = context.Request["b"] ?? "";
            if (branch != "")
            {
                string strQueryUser = context.Request["QUSER"] ?? UserInfo.User.UserName;
                if (strQueryUser == "")
                {
                    strQueryUser = new JH_Auth_BranchB().GetAllUserByBranch(branch);
                } 
                strWhere += string.Format("   jf.CRUser in ('{0}')", strQueryUser.ToFormatLike());
            }
            else
            {
                strWhere += string.Format("  jf.CRUser='{0}' ", UserInfo.User.UserName);
            }
            if (P1 != "")
            {
                strWhere += string.Format("and jf.BXContent like '%{0}%'", P1);
            }
            int page = 0;
            int.TryParse(P2, out page);
            page = page == 0 ? 1 : page;
            int total = 0;
            string colName = @"jf.*,
                            case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                            when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName";
            DataTable dt = new ZH_XTGGB().GetDataPager(" ZH_JFBX jf inner join  Yan_WF_PI wfpi  on jf.intProcessStanceid=wfpi.ID ", colName, 8, page, "jf.CRDate desc", strWhere, ref total);
            msg.Result = dt;
            msg.Result1 =total;
        }
        /// <summary>
        /// 获取经费报销列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">报销内容</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETJFBXLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strSql = string.Format(@"SELECT jf.ID,jf.BXZJE,jf.ShenQingRen,jf.FormCode,jf.BXDate,jf.BXContent,jf.BranchName, 
                                            case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                                            when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName  ,intProcessStanceid
                                            from ZH_JFBX jf inner join  Yan_WF_PI wfpi  on jf.intProcessStanceid=wfpi.ID ");
            string strWhere = " Where 1=1 ";
            if (P1 != "")
            {
                strWhere += string.Format(" And jf.BXContent like '%{0}%'", P1);
            }

            msg.Result = new ZH_JFBXB().GetDTByCommand(strSql + strWhere + " order by jf.crdate desc");
        }
        /// <summary>
        /// 获取经费报销列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">报销内容</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETJFBXLIST_PAGE(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = "";
            if (P1 != "")
            {
                strWhere += string.Format(" And jf.BXContent like '%{0}%'", P1);
            }
            int page = 0;
            int.TryParse(P2, out page);
            page = page == 0 ? 1 : page;
            int total = 0;
            string colName = @"jf.ID,jf.BXZJE,jf.ShenQingRen,jf.FormCode,jf.BXDate,jf.BXContent, 
                            case WHEN wfpi.isComplete is null and wfpi.IsCanceled is null  THEN '正在审批' 
                            when wfpi.isComplete='Y' then '已审批'  WHEN wfpi.IsCanceled='Y' then '已退回' END StateName,intProcessStanceid";
            DataTable dt = new ZH_XTGGB().GetDataPager("ZH_JFBX jf inner join  Yan_WF_PI wfpi  on jf.intProcessStanceid=wfpi.ID ", colName, 8, page, " jf.CRDate desc", strWhere, ref total);
            msg.Result = dt;
        }
        /// <summary>
        /// 删除经费报销单
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">Id</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELJFBXBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                ZH_JFBX jfbx = new ZH_JFBXB().GetEntity(d => d.ID.ToString() == P1);
                if (new ZH_JFBXB().Delete(d => d.ID.ToString() == P1))
                {
                    new ZH_JFBX_ItemB().GetEntities(d => jfbx.ItemIds.Split(',').Contains(d.ID.ToString()));
                    msg.ErrorMsg = "";
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
        /// <summary>
        /// 获取经费报销信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">Id</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETJFBXMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_JFBX jfbx = new ZH_JFBXB().GetEntity(d => d.ID.ToString() == P1);
            msg.Result = jfbx;
            msg.Result1 = new ZH_JFBX_ItemB().GetEntities(d => jfbx.ItemIds.Split(',').Contains(d.ID.ToString()));
            if (!string.IsNullOrEmpty(jfbx.Files))
            {
                msg.Result2 = new FT_FileB().GetEntities(d => jfbx.Files.Split(',').Contains(d.ID.ToString()));
            }
        }
        /// <summary>
        /// 获取经费报销详细
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">Id</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELJFBXITEM(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                if (new ZH_JFBX_ItemB().Delete(d => P1.Split(',').Contains(d.ID.ToString())))
                {
                    msg.ErrorMsg = "";
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }

    }
}